import * as THREE from "three";
const OrbitControls = require('three-orbit-controls')(THREE);

var House = function () {
  //视角
  var _camera;

  //控制器
  var _controls;

  //光源
  var _light;

  //场景
  var _scene;

  //渲染器
  var _renderer;

  //时间
  var _clock = new THREE.Clock();



  /////////////////////////////////////////////////////////////////////////
  //                         Init                                        //
  /////////////////////////////////////////////////////////////////////////
  this.Run = function () {
    animate();
  }

  //初始化
  this.Init = function (container, width, height, pixelRatio) {

    //初始化场景
    initScene();
    //初始化视角
    initCamera(width, height);
    //初始化光源
    initLight();

    //初始化渲染器
    initRenderer(width, height, pixelRatio);

    //初始化控制器
    initControls();

    container.appendChild(_renderer.domElement);

  };

   this.initGridHelper =function() {
    let gridHelper = new THREE.GridHelper(60, 60, 0xbbbbbb, 0x888888)
    this.Add(gridHelper)
  }
  //初始化渲染器
  function initRenderer(width, height, pixelRatio) {
    _renderer = new THREE.WebGLRenderer({
      antialias: true,            //开启反锯齿
      alpha: true,                //设置背景色透明
      preserveDrawingBuffer: true //保存绘图缓冲
    });
    _renderer.gammaInput = true;
    _renderer.gammaOutput = true;
    //
    _renderer.physicallyCorrectLights = true;
    //启动阴影
    _renderer.shadowMap.enabled = true;
    _renderer.shadowMap.type = THREE.PCFSoftShadowMap;

    _renderer.setPixelRatio(pixelRatio);
    //设置渲染器的宽和高
    _renderer.setSize(width, height);
    _renderer.setClearColor(0x444444, 1);
  }
  //初始化场景
  function initScene() {
    _scene = new THREE.Scene();
  }
  //初始化控制器
  function initControls() {
    _controls = new OrbitControls(_camera, _renderer.domElement);
  }
  //初始化视角
  function initCamera(width, height) {
    _camera = new THREE.PerspectiveCamera(50, width / height, 1, 10000);
    _camera.position.set(15, 10, 20);
    var dirLight = new THREE.DirectionalLight(0xffffff, 0.5);
    dirLight.position.set(10, 10, 10).normalize();
    _scene.add(dirLight);
    _scene.add(_camera);
  }
  //光源
  function initLight() {
    var hemiLight = new THREE.HemisphereLight(0xffffff, 0x0f0e0d, 4.2);
    //底部水平光
    var light = new THREE.DirectionalLight(0xffffff, 2.6);
    light.position.set(0, -1, 0);
    light.castShadow = false;
    _scene.add(light);
    _scene.add(hemiLight);
  }
  /////////////////////////////////////////////////////////////////////////
  //                         Draw                                        //
  /////////////////////////////////////////////////////////////////////////
  function animate() {
    requestAnimationFrame(animate);
    render();
  }
  //渲染
  function render() {
    _renderer.render(_scene, _camera);
  };
  /////////////////////////////////////////////////////////////////////////
  //                         Event                                       //
  /////////////////////////////////////////////////////////////////////////
  //设置显示区域
  this.Resize = function (width, height) {
    _camera.aspect = width / height;
    _camera.updateProjectionMatrix();
    _renderer.setSize(width, height);
  };
  this.Add = function (model) {
    _scene.add(model);
  };
  this.Remove = function (model) {
    _scene.remove(model);
  };

  this.SetBackground = function (color) {
    _renderer.setClearColor(color, 1);
  }



  /* 子模型拾取 */
  this.Raycaster = function (event, container, mesh) {
    event.preventDefault();

    var rect = container.getBoundingClientRect();

    var meshIndex;
    var x = ((event.clientX- rect.left) / rect.width) * 2 - 1;
    var y = -((event.clientY - rect.top) / rect.height) * 2 + 1;

    //创建射线投射器对象
    var raycaster = new THREE.Raycaster();
    raycaster.setFromCamera(new THREE.Vector2(x, y), _camera);

    //返回射线选中的对象
    var intersects = raycaster.intersectObjects(mesh);

    if (intersects.length > 0) {
      mesh.forEach(function (obj, index) {
        if (intersects[0].object === obj) {
          meshIndex = index;
        }
      });
      return meshIndex;
    }
  }

}



export default House;

